"""
    http://projecteuler.net/problem=4
    
    A palindromic number reads the same both ways. The largest palindrome made
    from the product of two 2-digit numbers is 9009 = 91 * 99.
    
    Find the largest palindrome made from the product of two 3-digit numbers.

"""
import time



def timeit(f):
    def timer():
        t0 = time.time()
        returned = f()
        print "time %s: %.6f" % (f, time.time() - t0)
        return returned
    return timer


@timeit    
def test_case():
    expected = 9009
    product, i, j = brutish_search(99, 10)
    
    assert product == expected, "product (%s) != expected %s" % (
            product, expected)
    print "test case succeeded! %d (%d * %d)" % (product, i, j)

    
@timeit
def solution():
    return brutish_search(999, 100)


def is_palindrome(n):
    def reversed(list_):
        list_.reverse()
        return list_
    
    s = str(n)
    return list(s) == reversed(list(s))

    
def brutish_search(max_, min_):
    palindrome = None
    
    for i in range(max_, min_, -1):
        #print i
        for j in range(max_, min_, -1):
            product = i * j
            
            if palindrome and product < palindrome[0]:
                break
            
            if is_palindrome(product):
                palindrome = [product, i, j]
            
        if palindrome and i * max_ < palindrome[0]:
            break
    
    return palindrome
    


if __name__ == '__main__':
    test_case()
    print solution()
    